#!/bin/sh

#
#   COPYRIGHT 2003-2006, EMULEX CORPORATION                               
#   3333 Susan St., Costa Mesa, CA 92626                                  
#                                                                         
#   All rights reserved.  This computer program and related documentation 
#   is protected by copyright  and distributed under licenses restricting 
#   its use,  copying,  distribution  and decompilation.    This computer 
#   program  and its  documentation  are CONFIDENTIAL  and a TRADE SECRET 
#   of EMULEX CORPORATION.   The receipt or  possession of  this  program 
#   or its documentation does not  convey rights to reproduce or disclose 
#   its  contents,  or to  manufacture, use, or sell anything that it may 
#   describe, in whole or in part,  without the specific  written consent 
#   of  EMULEX CORPORATION.   Any reproduction  of  this program  without 
#   the express  written  consent  of EMULEX  CORPORATION  is a violation 
#   of the  copyright laws  and may  subject you to  criminal prosecution.

#
# This script uninstalls HBAnyware or HBAnywareSSC for Linux
#
                                                                                                                        

opt="$1"

SSCUninstall=0
LOGFILE="/usr/src/lpfcdfc/utils-install.log"

# Function:  log_message()
#
# Description:
#   Print the strings passed as parameters to both STDOUT as well as
#   to a log file.
#
# Parameters:
#   A series of strings.
#
# Returns:
#   0 on success.
log_message()
{
    local ECHO_OPTIONS
    LOGDIRECTORY=${LOGFILE%/*}
    if [ ! -d ${LOGDIRECTORY} ] ; then
	mkdir -p ${LOGDIRECTORY}
    fi

    if [ $# -eq 0 ] ; then
	echo "" | tee -a ${LOGFILE}
    elif [ -z "$1" ] ; then
	echo "" | tee -a ${LOGFILE}
    elif [ "$1" = "-init" ] ; then
	echo "$0 - $(date)" >> ${LOGFILE}
    else
	while [ -n "$1" ] ; do
	    if [ "${1:0:1}" = "-" ] ; then
		ECHO_OPTIONS="${1}"
	    else
		echo ${ECHO_OPTIONS} "$1" | tee -a ${LOGFILE}
	    fi
	    shift
	done
    fi

    return 0
}

# Initialize the log file.
log_message -init

# Check if HBAnywareSSC uninstall option is passed in
if [ -n "$opt" ];then
    opt=`echo $opt | tr A-Z a-z`
    if [ "$opt" = "ssc" ];then
        SSCUninstall=1
    fi
fi

# Get environment data
OS=`uname -s`
Arch=`uname -m`
if [ "$Arch" = "i686" ];then
    Arch="i386"
fi
if [ "$Arch" = "x86_64" ];then
    Arch="i386"
    X86_64=1
else
    X86_64=0
fi

# init to default value
GCC_Ver="3"

if [ -f /etc/redhat-release ]; then
    rhel_major_version=`cat /etc/redhat-release | awk '{ print $7 }' | awk -F. '{ print $1 }'`

    if [ "$rhel_major_version" -eq 4 ];then
        # is this rhel5 Tikanga release
        rhel5_check=`cat /etc/redhat-release | grep Tikanga`
        if [ -n "$rhel5_check" ];then
           OS_Type="RH5"
           OS_Ver="rhel-5.0"
           OS_Ver_dfc="rhel-5.0"
           GCC_Ver="4"
        else
           OS_Type="RH4"
           OS_Ver="rhel-4.0"
           OS_Ver_dfc="rhel-4.0"
           GCC_Ver="3"
        fi
    else
        OS_Type="RH5"
        OS_Ver="rhel-5.0"
        OS_Ver_dfc="rhel-5.0"
        GCC_Ver="4"
    fi
elif [ -f /etc/SuSE-release ]; then
    sles_major_version=`cat /etc/SuSE-release | grep VERSION | awk '{ print $3 }'`
    if [ "$sles_major_version" -eq 10 ];then
        # is this sles10 release
        OS_Type="SL10"
        OS_Ver="sles-10.0"
        sles_minor_version=`cat /etc/SuSE-release | grep PATCHLEVEL | awk '{ print $3 }'`
        if [ "$sles_minor_version" = "1" ];then
            OS_Ver_dfc="sles-10-sp1"
        else
            OS_Ver_dfc="sles-10.0"
        fi
        GCC_Ver="4"
    else
        OS_Type="SL9"
        OS_Ver="sles-9.0"
        OS_Ver_dfc="sles-9.0"
        GCC_Ver="3"
    fi
else
    echo "ERROR: Cannot determine Linux distribution."
    exit
fi

                                                                                                                          
# This is a Linux install only, if we're not on Linux then stop
if [ "$OS" != "Linux" ];then
    log_message "This installation is for Linux only! Exiting..."
    exit
fi


# HBAnyware Uninstall
if [ $SSCUninstall -eq 0 ];then

    # Uninstall any previousely installed HBAnywareSSC
    Cur_HBA=`rpm -qa | grep HBAnywareSSC`
    if [ -n "$Cur_HBA" ];then
        stay_in_loop=1
        while [ $stay_in_loop -eq 1 ]
        do 
           echo
           echo "HBAnyware SSC package currently installed on this machine."
           echo "Must first uninstall HBAnyware SSC package before uninstalling HBAnyware."
           echo "Do you want to uninstall HBAnyware SSC package now?" 
           echo
           echo
           echo -n "Enter the letter 'y' if yes, or 'n' if no "

           # Obtain user selection for "uninstall SSC?" reply
           read ssc_uninstall
           if [ "$ssc_uninstall" = "y" ];then
               log_message "Uninstalling HBAnywareSSC -- $Cur_HBA -- ..."
               rpm -e "$Cur_HBA"
               stay_in_loop=0;
           fi
           if [ "$ssc_uninstall" = "Y" ];then
               log_message "Uninstalling HBAnywareSSC -- $Cur_HBA -- ..."
               rpm -e "$Cur_HBA"
               stay_in_loop=0;
           fi
           if [ "$ssc_uninstall" = "n" ];then
               log_message "HBAnyware not uninstalled."
               stay_in_loop=0;
			   exit
           fi
           if [ "$ssc_uninstall" = "N" ];then
               log_message "HBAnyware not uninstalled."
               stay_in_loop=0;
			   exit
           fi
        done
        echo ""
    fi

    # Backup existing config files
    log_message "Back up existing configuration files"
    rm -f /etc/emulexRMConfig.bak
    rm -f /etc/emulexRMOptions.bak
    rm -f /etc/emulexDiscConfig.bak
#    if [ -f /etc/emulexRMConfig ]; then
#        mv -f /etc/emulexRMConfig /etc/emulexRMConfig.bak
#    fi
#    if [ -f /etc/emulexRMOptions ]; then
#        mv -f /etc/emulexRMOptions /etc/emulexRMOptions.bak
#    fi
    if [ -f /etc/emulexDiscConfig ]; then
        mv -f /etc/emulexDiscConfig /etc/emulexDiscConfig.bak
    fi

    # uninstall HBAnyware
    Cur_HBA=`rpm -qa | grep HBAnyware | grep -v SSC`
    if [ -n "$Cur_HBA" ];then
        log_message "Uninstalling HBAnyware -- $Cur_HBA -- ..."
        rpm -e --nodeps "$Cur_HBA"
        log_message ""
    fi

# AppsLite elxlinuxcorekit Uninstall
    Cur_HBA=`rpm -qa | grep elxlinuxcorekit | grep -v SSC`
    if [ -n "$Cur_HBA" ];then
        echo "Uninstalling elxlinuxcorekit -- $Cur_HBA -- ..."
        rpm -e --nodeps "$Cur_HBA"
        echo ""
    fi

    # restore any pre-existing config files
    #log_message "Restore configuration files"
#    if [ -f /etc/emulexRMConfig.bak ]; then
#        mv -f /etc/emulexRMConfig.bak /etc/emulexRMConfig
#    fi
#    if [ -f /etc/emulexRMOptions.bak ]; then
#        mv -f /etc/emulexRMOptions.bak /etc/emulexRMOptions
#    fi
    if [ -f /etc/emulexDiscConfig.bak ]; then
        mv -f /etc/emulexDiscConfig.bak /etc/emulexDiscConfig
    fi

    rpm_test=`rpm -qa | grep "libdfc-"`
    if [ -n "$rpm_test" ];then
        log_message "Uninstalling DFC Library RPM files ..."
        rm -f /usr/sbin/lpfc/lun_scan
        # Uninstall dfc libs and utility
        Cur_DFC=`rpm -qa | grep libdfc-64bit`
        if [ -n "$Cur_DFC" ];then
            log_message "Uninstalling libdfc -- $Cur_DFC -- ..."
            rpm -e "$Cur_DFC"
        fi
        Cur_DFC=`rpm -qa | grep libdfc`
        if [ -n "$Cur_DFC" ];then
            log_message "Uninstalling libdfc -- $Cur_DFC -- ..."
            rpm -e "$Cur_DFC"
        fi
        rm -f /usr/lib/libmpl.so
        rm -f /usr/lib64/libmpl.so
    else
         # Uninstall dfc libs and utility
         log_message "Uninstalling DFC Library files ..."
         rm -f /usr/sbin/lpfc/dfc
         rm -f /usr/lib/libdfc.so
         rm -f /usr/lib/libmpl.so
         rm -f /usr/lib/libdfc.a
         rm -f /usr/lib64/libdfc.so
         rm -f /usr/lib64/libmpl.so
         rm -f /usr/lib64/libdfc.a
         rm -f /usr/sbin/lpfc/lun_scan
    fi
    log_message ""

    # Uninstall HBAAPI libs
    rm -f /usr/lib/libHBAAPI.so
    rm -f /usr/lib/libemulexhbaapi.so
    rm -f /usr/lib64/libHBAAPI.so
    rm -f /usr/lib64/libemulexhbaapi.so

    # Uninstall lputil utility
    rm -f /usr/sbin/lpfc/lputil

    # remove semaphore lock file directory
    rm -rf /usr/sbin/hbanyware/misc

    # Uninstall JRE
    rm -rf /usr/sbin/hbanyware/jre

    rm -f /usr/sbin/hbanyware/READ*.txt

   # Uninstall dfc helper module
    cd /usr/sbin/hbanyware/scripts

# don't install lpfcdfc for rhel5
if [ "$OS_Type" != "RH5" ]; then
   if [ "$OS_Ver_dfc" != "sles-10-sp1" ]; then
      ./lpfcdfc-install -u
      EXITCODE=$?
      if [ ${EXITCODE} -eq 10 ];then
          log_message ""
          log_message "Success with WARNINGS uninstalling Application Helper"
          log_message "Module.  Please refer to the following file for more"
          log_message "information about these warnings:"
          log_message "   ${LOGFILE}"
          log_message ""
      elif [ ${EXITCODE} -ne 0 ];then
        log_message "ERROR: Application Helper Module uninstallation failed."
      fi
    fi
fi
    rm -rf /usr/sbin/hbanyware/scripts

    rmdir --ignore-fail-on-non-empty /usr/sbin/hbanyware
    rmdir --ignore-fail-on-non-empty /usr/sbin/lpfc
    rm -f /usr/lib/lib_unix-rm/lib_rm_unix.so
    rm -f /usr/lib64/lib_unix-rm/lib_rm_unix.so

    log_message "HBAnyware Uninstallation complete."
    log_message ""

# don't uninstall lpfcdfc for rhel5
if [ "$OS_Type" != "RH5" ]; then
  if [ "$OS_Ver_dfc" != "sles-10-sp1" ]; then
    # Uninstall dfc helper module
    cd enterprise_kitfiles
    ./lpfcdfc-install -u
    EXITCODE=$?
    if [ ${EXITCODE} -eq 10 ];then
	log_message ""
	log_message "Success with WARNINGS uninstalling Application Helper"
	log_message "Module.  Please refer to the following file for more"
	log_message "information about these warnings:"
	log_message "   ${LOGFILE}"
	log_message ""
    elif [ ${EXITCODE} -ne 0 ];then
        log_message "ERROR: Application Helper Module uninstallation failed."
    fi
	cd ..
  fi
fi

#  HBAnywareSSC Uninstallation
else   
    # Uninstall any previousely installed HBAnywareSSC
    Cur_HBA=`rpm -qa | grep HBAnywareSSC`
    if [ -n "$Cur_HBA" ];then
        log_message "Uninstalling HBAnywareSSC -- $Cur_HBA -- ..."
        rpm -e "$Cur_HBA"
    fi

    log_message "HBAnywareSSC Uninstallation complete."
    log_message ""
fi


